package com.kyh.service.sys.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kyh.enume.sys.ColEnum;
import com.kyh.properties.AppProps;
import com.kyh.service.sys.ColService;
import com.kyh.sys.entity.Col;
import com.kyh.sys.entity.Columns;
import com.kyh.sys.entity.Menu;
import com.kyh.sys.mapper.ColMapper;
import com.kyh.sys.mapper.MenuMapper;
import com.kyh.util.CheckUtil;
import com.kyh.vo.BootstrapTableVoJA;
import com.kyh.vo.R;

import tk.mybatis.mapper.util.StringUtil;

@Service("colService")
public class ColServiceImpl implements ColService {

	@Autowired
	private ColMapper colMapper;
	@Autowired
	private MenuMapper menuMapper;

	@Override
	@Transactional
	public R add(Col entity) throws Exception {
		Integer colStatusAdd = entity.getColStatusAdd();
		Integer colStatusEdit = entity.getColStatusEdit();
		
		// 验证所属菜单
		Long menuId = entity.getMenuId();
		if (!CheckUtil.notNull(menuId)) {
			return R.error("请选择菜单");
		}
		
		// 验证菜单-编码是否已存在
		String colCode = entity.getColCode();
		Col colCheck = colMapper.getByUk(menuId, colCode);
		if (colCheck != null) {
			return R.error("该菜单的此字段已存在");
		}
		
		// 验证是否主键
		Menu menu = menuMapper.get(menuId);
		String menuTable = menu.getMenuTable();
		String tableSchema = AppProps.get("jdbc.databaseName");
		if (menuTable.startsWith("sys_")) {
			tableSchema = AppProps.get("admin.jdbc.databaseName");
		}
		Columns columns = new Columns().setTableSchema(tableSchema).setTableName(menuTable)
				.setColumnName(StringUtil.camelhumpToUnderline(entity.getColCode())).queryFirst();
		if (columns != null && "PRI".equals(columns.getColumnKey())) {
			entity.setColStatusAdd(ColEnum.StatusAdd.NO.value);
			entity.setColStatusEdit(ColEnum.StatusEdit.NO.value);
			entity.setColScripts(null);
			entity.setColSelectors(null);
			entity.setColStatusValidate(ColEnum.StatusValidate.NO.value);
			entity.setColSortNo(0);
			entity.setColStatusListEdit(ColEnum.StatusListEdit.NO.value);
			entity.setColName("主键");
			entity.setColType(ColEnum.ColType.TEXT.value);
			entity.setColValueOnload(null);
			
			// 添加数据
			if (colMapper.insert(entity) != 1) {
				throw new RuntimeException("失败");
			}
			return R.ok();
		}
		
		
		// 验证选择框
		Integer colType = entity.getColType();
		String colSelectors = entity.getColSelectors();
		if (colType == ColEnum.ColType.SELECTOR.value) {
			if (colSelectors == null || colSelectors.length() == 0 || !colSelectors.contains("_")) {
				return R.error("类型为选择框时，内容格式不正确");
			}
		}
		
		// 图片字段不能作为搜索字段
		Integer colStatusSearch = entity.getColStatusSearch();
		if (colType == ColEnum.ColType.IMG.value && colStatusSearch == ColEnum.StatusSearch.YES.value) {
			return R.error("图片类型不能作为搜索字段");
		}
		
		// 带默认值的字段不能作为搜索条件
		String colValueOnload = entity.getColValueOnload();
		if (colStatusSearch == 1 && (colValueOnload != null && colValueOnload.length() > 0)) {
			return R.error("带默认值的字段不能作为搜索条件");
		}
		
		// 带默认值的字段不能编辑
		if (colStatusEdit == ColEnum.StatusEdit.YES.value && (colValueOnload != null && colValueOnload.length() > 0)) {
			return R.error("带默认值的字段不能编辑");
		}
		
		// 带默认值的字段不能验证
		Integer colStatusValidate = entity.getColStatusValidate();
		if (colStatusValidate == ColEnum.StatusValidate.YES.value && (colValueOnload != null && colValueOnload.length() > 0)) {
			return R.error("带默认值的字段不能验证");
		}
		
		// 验证是否新增
		if (colStatusAdd == ColEnum.StatusAdd.YES.value) {
			Columns syscolumn = new Columns().setTableSchema(tableSchema)
					.setTableName(menuTable)
					.setColumnName(StringUtil.camelhumpToUnderline(colCode)).queryFirst();
//					syscolumnMapper.get(AppProps.get("jdbc.databaseName"), menuTable, colCode);
			if (syscolumn == null) {
				entity.setColStatusAdd(ColEnum.StatusAdd.NO.value);
			}
		}
		
		// 验证是否编辑
		if (colStatusAdd == ColEnum.StatusAdd.YES.value) {
			Columns syscolumn = new Columns().setTableSchema(tableSchema)
					.setTableName(menuTable)
					.setColumnName(StringUtil.camelhumpToUnderline(colCode)).queryFirst();
			if (syscolumn == null) {
				entity.setColStatusEdit(ColEnum.StatusEdit.NO.value);
			}
		}
		
		// 添加数据
		if (colMapper.insert(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R edit(Col entity) throws Exception {
		Integer colType = entity.getColType();
		String colSelectors = entity.getColSelectors();
		Integer colStatusSearch = entity.getColStatusSearch();
		String colValueOnload = entity.getColValueOnload();
		Integer colStatusAdd = entity.getColStatusAdd();
		Integer colStatusEdit = entity.getColStatusEdit();
		Integer colStatusValidate = entity.getColStatusValidate();
		Long menuId = entity.getMenuId();
		Long colId = entity.getColId();
		String colCode = entity.getColCode();
		
		// 验证菜单-编码是否已存在
		if (menuId != null && colCode != null) {
			Col colCheck = colMapper.getByUk(menuId, colCode);
			if (colCheck != null && colId != null && colId.longValue() != colCheck.getColId().longValue()) {
				return R.error("该菜单的此字段已存在");
			}
		}
		
		// 验证是否主键
		Menu menu = menuMapper.get(menuId);
		String menuTable = menu.getMenuTable();
		String tableSchema = AppProps.get("jdbc.databaseName");
		if (menuTable.startsWith("sys_")) {
			tableSchema = AppProps.get("admin.jdbc.databaseName");
		}
		Columns columns = new Columns().setTableSchema(tableSchema).setTableName(menuTable)
				.setColumnName(StringUtil.camelhumpToUnderline(entity.getColCode()))
				.queryFirst();
		if (columns != null && "PRI".equals(columns.getColumnKey())) {
			entity.setColStatusAdd(ColEnum.StatusAdd.NO.value);
			entity.setColStatusEdit(ColEnum.StatusEdit.NO.value);
			entity.setColScripts(null);
			entity.setColSelectors(null);
			entity.setColStatusValidate(ColEnum.StatusValidate.NO.value);
			entity.setColSortNo(0);
			entity.setColStatusListEdit(ColEnum.StatusListEdit.NO.value);
			entity.setColName("主键");
			entity.setColType(ColEnum.ColType.TEXT.value);
			entity.setColValueOnload(null);
			
			// 更新数据
			if (colMapper.update(entity) != 1) {
				throw new RuntimeException("失败");
			}
			return R.ok();
		}
		
		// 验证选择框
		if (colType != null && colType == ColEnum.ColType.SELECTOR.value) {
			if (colSelectors == null || colSelectors.length() == 0 || !colSelectors.contains("select")) {
				return R.error("类型为选择框时，内容格式不正确");
			}
		}
		
		// 图片字段不能作为搜索字段
		if (colType!= null && colType == 4 && colStatusSearch!= null && colStatusSearch == ColEnum.StatusSearch.YES.value) {
			return R.error("图片类型不能作为搜索字段");
		}
		
		// 带默认值的字段不能作为搜索条件
		if (colStatusSearch != null && colStatusSearch == ColEnum.StatusSearch.YES.value && (colValueOnload != null && colValueOnload.length() > 0)) {
			return R.error("带默认值的字段不能作为搜索条件");
		}
		
		// 带默认值的字段不能编辑
		if (colStatusEdit != null && colStatusEdit == ColEnum.StatusEdit.YES.value && (colValueOnload != null && colValueOnload.length() > 0)) {
			return R.error("带默认值的字段不能编辑");
		}
		
		// 带默认值的字段不能验证
		if (colStatusValidate != null && colStatusValidate == ColEnum.StatusValidate.YES.value && (colValueOnload != null && colValueOnload.length() > 0)) {
			return R.error("带默认值的字段不能验证");
		}
		
		// 验证是否新增
		if (colStatusAdd == ColEnum.StatusAdd.YES.value) {
			Columns syscolumn = new Columns().setTableSchema(tableSchema)
					.setTableName(menuTable)
					.setColumnName(StringUtil.camelhumpToUnderline(colCode)).queryFirst();
			if (syscolumn == null) {
				entity.setColStatusAdd(ColEnum.StatusAdd.NO.value);
			}
		}
		
		// 验证是否编辑
		if (colStatusEdit == ColEnum.StatusEdit.YES.value) {
			Columns syscolumn = new Columns().setTableSchema(tableSchema)
					.setTableName(menuTable)
					.setColumnName(StringUtil.camelhumpToUnderline(colCode)).queryFirst();
			if (syscolumn == null) {
				entity.setColStatusEdit(ColEnum.StatusEdit.NO.value);
			}
		}
		
		// 更新数据
		if (colMapper.update(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteFalse(Long colId) throws Exception {
		
		if (colMapper.deleteFalse(colId) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteTrue(Long colId) {
		
		colMapper.deleteTrue(colId);
		
		return R.ok();
	}

	@Override
	public BootstrapTableVoJA search(Col cond) {
		
		List<Map<String, Object>> listMap = cond.queryMap();
		
		int total = cond.count();
		
        BootstrapTableVoJA vo = new BootstrapTableVoJA();
        vo.setRowsByListMap(listMap);
        vo.setTotal(total);
        
        return vo;
	}
}
